const app = getApp()
const request = require('../../utils/https.js')
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHideToast: false,
    navList: ['注册会员', 'VIP', 'SVIP'],
    active: 0,
    inputValue: '', //搜索的内容
    pageNum:1,
    search:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    userId = wx.getStorageSync('userId');
    //管家列表
    request._get(getApp().Api_url + 'rank/pick', {
      userId: userId,
      pageNum: that.data.pageNum,
      pageSize: 10
    }, function(res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        that.setData({
          DataIntergral: res.data.data
        })
      }
    }, function() {})
  },
  goShopping() {
    wx.switchTab({
      url: '/pages/list/list',
    })
  },
  //选择管家
  onChoose(e) {
    const that = this;
    let userId = wx.getStorageSync('userId');
    let fatherId = e.currentTarget.dataset.fatherid;
    request._get(getApp().Api_url + 'rank/goFor', {
      userId: userId,
      fromUserId: fatherId

    }, function(res) {
      if (res.data.errMsg == 'OK') {
        console.log('选择管家==>', res.data.data)
        wx.showToast({
          title: '选择成功',
        })

        wx.switchTab({
          url: '/pages/list/list',
        })

      }
    }, function() {})
  },
  //自定义弹框
  onToast() {
    const that = this;
    that.setData({
      isHideToast: true
    })
  },
  shutToast() {
    const that = this;
    that.setData({
      isHideToast: false
    })
  },
  onChange(e) {
    const that = this;
    console.log('e.detail===>', e.detail)
    if (!e.detail) {
      console.log('空')
      that.setData({ //初始化page
        pageNum: 1,
        search:false
      });
      request._get(getApp().Api_url + 'rank/pick', {
        userId: userId,
        pageNum: that.data.pageNum,
        pageSize: 10
      }, function(res) {
        if (res.data.errMsg == 'OK') {
          console.log('管家列表==>', res.data.data)
          that.setData({
            DataIntergral: res.data.data
          })
        }
      }, function() {})
    }else{
      console.log('不空')
      that.setData({
        search:true
      })
    }


  },
  //搜索功能
  onSearch(e) {
    const that = this;
    request._get(getApp().Api_url + 'rank/pickSearch', {
      nickName: e.detail,
      userId: userId,
      pageNum: 1,
      pageSize: 30
    }, function(res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        that.setData({
          DataIntergral: res.data.data
        })
      }
    }, function() {})
  },
  // 下拉刷新回调接口
  onPullDownRefresh: function () {
    const that = this;
    that.setData({ //初始化page
      pageNum: 1
    });
    wx.stopPullDownRefresh();
    that.onLoad();
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const that=this;

   // console.log('搜索框是否有东西===>',)
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    userId = wx.getStorageSync('userId');
    //管家列表


    if(that.data.search){
      var url='rank/pickSearch'
    }else{
      var url='rank/pick'
    }
    request._get(getApp().Api_url + url, {
      userId: userId,
      pageNum: that.data.pageNum,
      pageSize: 10
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        console.log('that.data.DataIntergral==>', that.data.DataIntergral)

        let DataIntergral = that.data.DataIntergral;
        let newDataIntergral = res.data.data;
        that.setData({
          DataIntergral: DataIntergral.concat(newDataIntergral)
        })
      }
    }, function () { })
  }
})